<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Batch extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('flexigrid');
        $this->load->library('flexigrid');
		$this->load->library('session');
        $this->load->model('batch_model');
        $this->load->model('lisensi_model');
		$this->load->library('form_validation');
		$this->cek_login();
    }

	
	function cek_login(){
		$id_hak = $this->session->userdata('id_hak');
		if($id_hak < 1 || $id_hak > 2 || $id_hak == ''){
			redirect ('login');
		}
		
	}
	

	function daftar($id_lisensi){
		$this->session->set_userdata('id_lisensi', $id_lisensi);
		
		$record_data = $this->lisensi_model->selectone($id_lisensi);
		foreach ($record_data->result() as $lisensi)
		{
			$this->session->set_userdata('nama_lisensi', $lisensi->NAMA_LISENSI);
		}
		
		$this->index();
	}
	
    //load Grid
    function index() {
        $colModel['no'] = array('No', 30, TRUE, 'center', 0);
        $colModel['Nama_Batch'] = array('Nama Angkatan atau Batch', 130, TRUE, 'center', 1);
        $colModel['Tanggal_Entry'] = array('Tanggal Mulai', 90, TRUE, 'center', 1);
        $colModel['Tanggal_Selesai'] = array('Tanggal Selesai', 90, TRUE, 'center', 1);
        $colModel['Jam'] = array('Jam', 70, TRUE, 'center', 1);
        $colModel['Tempat'] = array('Tempat', 100, TRUE, 'center', 1);
        $colModel['Tanggal_Booking'] = array('Tanggal Booking', 100, TRUE, 'center', 1);
        $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);
        if($this->session->userdata('id_hak') == 1){
            $colModel['Approve_Batch'] = array('Approve Batch', 80, TRUE, 'center', 0);
        }else{
            $colModel['Status_Approve'] = array('Status Approve', 80, TRUE, 'center', 0);
        }
		$colModel['Daftar_Peserta'] = array('Daftar Peserta', 70, TRUE, 'center', 0);
		
		
        


        $gridParams = array(
            'width' => 'auto',
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Angkatan atau Batch',
            'showTableToggleBtn' => true
        );

        //$buttons[] = array('Tambah','add','test');
        //$buttons[] = array('separator');

        $buttons[] = array('Tambah', 'add', 'spt_js');
        $buttons[] = array('separator');
        $buttons[] = array('Hapus', 'delete', 'spt_js');

        // mengambil data dari file controler ajax pada method grid_berkas
        if($this->session->userdata('id_hak')==1){
            $grid_js = build_grid_js('flex1', site_url("batch/batch/grid_batch_admin"), $colModel, 'Nama_Batch', 'asc', $gridParams, $buttons);
        }else{
            $grid_js = build_grid_js('flex1', site_url("batch/batch/grid_batch"), $colModel, 'Nama_Batch', 'asc', $gridParams, $buttons);
		}
        


        //additional JS
        $data['added_js'] =
                "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/batch/batch/form_batch'; 
			}
			
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data batch?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/batch/batch/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
				
		} 
		</script>
		";

        $data['js_grid'] = $grid_js;
        //remdering view
        $data['title'] = 'Daftar Batch';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'batch/grid_batch', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    function grid_batch() {
        $valid_fields = array('Nama_Lisensi','Nama_Batch', 'Tanggal_Entry', 'Tanggal_Selesai');
        $this->flexigrid->validate_post('Nama_Batch', 'asc', $valid_fields);
        $records = $this->batch_model->get_data_batch($this->session->userdata('id_lisensi'));
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            
            //set status batch
            $status = '';
            
            
//            $link_edit = "";
//            $image_edit = "" . base_url() . "images/grid/edit_disable.png";
//            $link_daftar_peserta = "";
//            $image_daftar_peserta = "" . base_url() . "images/grid/peserta_disable.png";
//            if ($this->session->userdata('id_lisensi') == $row->ID_LISENSI) {
                $link_edit = "<a href='" . base_url() . "index.php/batch/batch/edit/" . $row->ID_BATCH . "'>";
                $image_edit = "" . base_url() . "images/grid/edit.png";
                $link_daftar_peserta = "<a href='" . base_url() . "index.php/peserta/peserta/daftar/" . $row->ID_BATCH . "'>";
                $image_daftar_peserta = "" . base_url() . "images/grid/peserta.png";
            //}
			
			if ($row->STATUS == 1) {
                $status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/not_yet.png\'>';
				$image_status_online = '<img border=\'0\' src=\'' . base_url() . 'images/grid/online.png\'>';
				$link_status_online = "<a href=#'>";
            } 
			else if ($row->STATUS == 3) {
				$status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/fix.png\'>';
				$image_status_online = '<img border=\'0\' src=\'' . base_url() . 'images/grid/offline.png\'>';
				$link_status_online = "<a href='" . base_url() . "index.php/batch/batch/go_online/" . $row->ID_BATCH . "'>";
				$link_daftar_peserta = "<a href=#'>";
				$link_edit = "<a href=#'>";
			}
			else {
                $status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/fix.png\'>';
				$image_status_online = '<img border=\'0\' src=\'' . base_url() . 'images/grid/online.png\'>';
				$link_status_online = "<a href='" . base_url() . "index.php/batch/batch/go_offline/" . $row->ID_BATCH . "'>";
            }
			
			
            
				
			//modifikasi tanggal entry
			$tanggal = substr($row->TANGGAL_ENTRY, -2);
			$bulan = substr($row->TANGGAL_ENTRY, -5, 2);
			$tahun = substr($row->TANGGAL_ENTRY, -10, 4);
			$tanggal_entry = '' . $tanggal . '-' . $bulan . '-' . $tahun . '';
			
			//modifikasi tanggal selesai
			$tanggal_s = substr($row->TANGGAL_SELESAI, -2);
			$bulan_s = substr($row->TANGGAL_SELESAI, -5, 2);
			$tahun_s = substr($row->TANGGAL_SELESAI, -10, 4);
			$tanggal_selesai = '' . $tanggal_s . '-' . $bulan_s . '-' . $tahun_s . '';
			
			
			//modifikasi tanggal booking
			$tanggal_b = substr($row->TANGGAL_BOOKING, -2);
			$bulan_b = substr($row->TANGGAL_BOOKING, -5, 2);
			$tahun_b = substr($row->TANGGAL_BOOKING, -10, 4);
			$tanggal_booking = '' . $tanggal_b . '-' . $bulan_b . '-' . $tahun_b . '';
			
            $record_items[] = array(
                $row->ID_BATCH,
                $no = $no + 1,
                $row->NAMA_BATCH,
				$tanggal_entry,
                $tanggal_selesai,
				$row->JAM,
				$row->TEMPAT,
                $tanggal_booking,
                '' . $link_edit . '<img border=\'0\' src=\'' . $image_edit . '\'></a> ',
                '' . $status . '</a> ',
                '' . $link_daftar_peserta . '<img border=\'0\' src=\'' . $image_daftar_peserta . '\'></a> '
                    //'<a href=\''.base_url().'index.php/peserta/peserta/daftar/'.$row->ID_BATCH.'\'><img border=\'0\' src=\''.base_url().'images/grid/peserta.png\'></a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
    
	//batch per lisensi untuk admin
    function grid_batch_admin() {
        $valid_fields = array('Nama_Batch', 'Tanggal_Entry', 'Tanggal_Selesai');
        $this->flexigrid->validate_post('Nama_Batch', 'asc', $valid_fields);
		//$records = $this->batch_model->get_data_batch($this->session->userdata('id_lisensi'));
		
		$records = $this->batch_model->get_data_batch($this->session->userdata('id_lisensi'));
        
		//$records = $this->batch_model->get_data_lisensi_batch($this->session->userdata('id_lisensi'));
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            
                $link_edit = "<a href='" . base_url() . "index.php/batch/batch/edit/" . $row->ID_BATCH . "'>";
                $image_edit = "" . base_url() . "images/grid/edit.png";
                $link_daftar_peserta = "<a href='" . base_url() . "index.php/peserta/peserta/daftar/" . $row->ID_BATCH . "'>";
                $image_daftar_peserta = "" . base_url() . "images/grid/peserta.png";  
                
                
            //set status batch
            $status = '';
            if ($row->STATUS == 1) {
                $status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/not_yet.png\'>';
            } else {
                $status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/fix.png\'>';
            }
			
			
			//modifikasi tanggal entry
			$tanggal = substr($row->TANGGAL_ENTRY, -2);
			$bulan = substr($row->TANGGAL_ENTRY, -5, 2);
			$tahun = substr($row->TANGGAL_ENTRY, -10, 4);
			$tanggal_entry = '' . $tanggal . '-' . $bulan . '-' . $tahun . '';
			
			//modifikasi tanggal selesai
			$tanggal_s = substr($row->TANGGAL_SELESAI, -2);
			$bulan_s = substr($row->TANGGAL_SELESAI, -5, 2);
			$tahun_s = substr($row->TANGGAL_SELESAI, -10, 4);
			$tanggal_selesai = '' . $tanggal_s . '-' . $bulan_s . '-' . $tahun_s . '';
            
			//modifikasi tanggal booking
			$tanggal_b = substr($row->TANGGAL_BOOKING, -2);
			$bulan_b = substr($row->TANGGAL_BOOKING, -5, 2);
			$tahun_b = substr($row->TANGGAL_BOOKING, -10, 4);
			$tanggal_booking = '' . $tanggal_b . '-' . $bulan_b . '-' . $tahun_b . '';

            $record_items[] = array(
                $row->ID_BATCH,
                $no = $no + 1,
                $row->NAMA_BATCH,
                $tanggal_entry,
                $tanggal_selesai,
				$row->JAM,
				$row->TEMPAT,
				$tanggal_booking,
                '' . $link_edit . '<img border=\'0\' src=\'' . $image_edit . '\'></a> ',
                '<a href=\'' . base_url() . 'index.php/batch/batch/aprove/' . $row->ID_BATCH . '\'>' . $status . '</a> ',
                '' . $link_daftar_peserta . '<img border=\'0\' src=\'' . $image_daftar_peserta . '\'></a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
	
	
	
	
	
	
	
	//--------all batch
	function all_batch() {
        $colModel['no'] = array('No', 30, TRUE, 'center', 0);
        $colModel['Nama_Lisensi'] = array('Nama Lisensi', 120, TRUE, 'center', 1);
        $colModel['Nama_Batch'] = array('Nama Angkatan atau Batch', 120, TRUE, 'center', 1);
        $colModel['Tanggal_Entry'] = array('Tanggal Mulai', 90, TRUE, 'center', 1);
        $colModel['Tanggal_Selesai'] = array('Tanggal Selesai', 90, TRUE, 'center', 1);
        $colModel['Jam'] = array('Jam', 70, TRUE, 'center', 1);
        $colModel['Tempat'] = array('Tempat', 100, TRUE, 'center', 1);
        $colModel['Tanggal_Booking'] = array('Tanggal Booking', 100, TRUE, 'center', 1);
        if($this->session->userdata('id_hak') == 1){
            $colModel['Approve_Batch'] = array('Approve Batch', 80, TRUE, 'center', 0);
        }else{
            $colModel['Status_Approve'] = array('Status Approve', 80, TRUE, 'center', 0);
        }
        


        $gridParams = array(
            'width' => 'auto',
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Angkatan atau Batch',
            'showTableToggleBtn' => true
        );

        $buttons[] = array('Hapus', 'delete', 'spt_js');

        // mengambil data dari file controler ajax pada method grid_berkas
            $grid_js = build_grid_js('flex1', site_url("batch/batch/grid_all_batch_admin"), $colModel, 'Nama_Lisensi', 'asc', $gridParams, $buttons);
        


        //additional JS
        $data['added_js'] =
                "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data batch?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/batch/batch/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
				
		} 
		</script>
		";

        $data['js_grid'] = $grid_js;
        //remdering view
        $data['title'] = 'Daftar Batch';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'batch/grid_batch', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }
	
	
	
	
	//grid all batch for admin
	function grid_all_batch_admin() {
        $valid_fields = array('Nama_Batch', 'Tanggal_Entry', 'Tanggal_Selesai');
        $this->flexigrid->validate_post('Nama_Batch', 'asc', $valid_fields);
        $records = $this->batch_model->get_data_all_lisensi_batch();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            
                $link_edit = "<a href='" . base_url() . "index.php/batch/batch/edit/" . $row->ID_BATCH . "'>";
                $image_edit = "" . base_url() . "images/grid/edit.png";
                $link_daftar_peserta = "<a href='" . base_url() . "index.php/peserta/peserta/daftar/" . $row->ID_BATCH . "'>";
                $image_daftar_peserta = "" . base_url() . "images/grid/peserta.png";  
                
                
            //set status batch
            $status = '';
            if ($row->STATUS == 1) {
                $status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/not_yet.png\'>';
            } else {
                $status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/fix.png\'>';
            }
			
			
			//modifikasi tanggal entry
			$tanggal = substr($row->TANGGAL_ENTRY, -2);
			$bulan = substr($row->TANGGAL_ENTRY, -5, 2);
			$tahun = substr($row->TANGGAL_ENTRY, -10, 4);
			$tanggal_entry = '' . $tanggal . '-' . $bulan . '-' . $tahun . '';
			
			//modifikasi tanggal selesai
			$tanggal_s = substr($row->TANGGAL_SELESAI, -2);
			$bulan_s = substr($row->TANGGAL_SELESAI, -5, 2);
			$tahun_s = substr($row->TANGGAL_SELESAI, -10, 4);
			$tanggal_selesai = '' . $tanggal_s . '-' . $bulan_s . '-' . $tahun_s . '';
			
			//modifikasi tanggal booking
			$tanggal_b = substr($row->TANGGAL_BOOKING, -2);
			$bulan_b = substr($row->TANGGAL_BOOKING, -5, 2);
			$tahun_b = substr($row->TANGGAL_BOOKING, -10, 4);
			$tanggal_booking = '' . $tanggal_b . '-' . $bulan_b . '-' . $tahun_b . '';
                

            $record_items[] = array(
                $row->ID_BATCH,
                $no = $no + 1,
                $row->NAMA_LISENSI,
                $row->NAMA_BATCH,
                $tanggal_entry,
                $tanggal_selesai,
				$row->JAM,
				$row->TEMPAT,
                $tanggal_booking,
                '<a href=\'' . base_url() . 'index.php/batch/batch/aprove/' . $row->ID_BATCH . '\'>' . $status . '</a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
	
	
	
	//booking batch
	function booking_batch() {
        $colModel['no'] = array('No', 30, TRUE, 'center', 0);
		$colModel['Tanggal_Booking'] = array('Tanggal Booking', 100, TRUE, 'center', 1);
		$colModel['Nama_Lisensi'] = array('Nama Lisensi', 150, TRUE, 'center', 1);
        $colModel['Nama_Batch'] = array('Nama Angkatan atau Batch', 150, TRUE, 'center', 1);
        $colModel['Tanggal_Entry'] = array('Tanggal Mulai', 100, TRUE, 'center', 1);
        $colModel['Tanggal_Selesai'] = array('Tanggal Selesai', 100, TRUE, 'center', 1);
        if($this->session->userdata('id_hak') == 1){
            $colModel['Approve_Batch'] = array('Approve Batch', 110, TRUE, 'center', 0);
        }else{
            $colModel['Status_Approve'] = array('Status Approve', 110, TRUE, 'center', 0);
        }
		
		
        


        $gridParams = array(
            'width' => 'auto',
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Booking Batch',
            'showTableToggleBtn' => true
        );

        //$buttons[] = array('Tambah','add','test');
        //$buttons[] = array('separator');

        $buttons[] = array('Booking', 'add', 'spt_js');

        // mengambil data dari file controler ajax pada method grid_berkas
		$grid_js = build_grid_js('flex1', site_url("batch/batch/grid_booking_batch"), $colModel, 'Tanggal_Booking', 'asc', $gridParams, $buttons);
        


        //additional JS
        $data['added_js'] =
                "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Booking')
			{
				location.href='" . base_url() . "index.php/batch/batch/form_batch'; 
			}
			
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data batch?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/batch/batch/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
				
		} 
		</script>
		";

        $data['js_grid'] = $grid_js;
        //remdering view
        $data['title'] = 'Daftar Batch';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'batch/grid_batch', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    function grid_booking_batch() {
        $valid_fields = array('Nama_Lisensi','Nama_Batch', 'Tanggal_Entry', 'Tanggal_Selesai');
        $this->flexigrid->validate_post('Nama_Batch', 'asc', $valid_fields);
        $records = $this->batch_model->get_data_all_lisensi_batch();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            
            //set status batch
            $status = '';
            
            
//            $link_edit = "";
//            $image_edit = "" . base_url() . "images/grid/edit_disable.png";
//            $link_daftar_peserta = "";
//            $image_daftar_peserta = "" . base_url() . "images/grid/peserta_disable.png";
//            if ($this->session->userdata('id_lisensi') == $row->ID_LISENSI) {
                $link_edit = "<a href='" . base_url() . "index.php/batch/batch/edit/" . $row->ID_BATCH . "'>";
                $image_edit = "" . base_url() . "images/grid/edit.png";
                $link_daftar_peserta = "<a href='" . base_url() . "index.php/peserta/peserta/daftar/" . $row->ID_BATCH . "'>";
                $image_daftar_peserta = "" . base_url() . "images/grid/peserta.png";
            //}
			
			if ($row->STATUS == 1) {
                $status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/not_yet.png\'>';
				$image_status_online = '<img border=\'0\' src=\'' . base_url() . 'images/grid/online.png\'>';
				$link_status_online = "<a href=#'>";
            } 
			else if ($row->STATUS == 3) {
				$status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/fix.png\'>';
				$image_status_online = '<img border=\'0\' src=\'' . base_url() . 'images/grid/offline.png\'>';
				$link_status_online = "<a href='" . base_url() . "index.php/batch/batch/go_online/" . $row->ID_BATCH . "'>";
				$link_daftar_peserta = "<a href=#'>";
				$link_edit = "<a href=#'>";
			}
			else {
                $status = '<img border=\'0\' src=\'' . base_url() . 'images/grid/fix.png\'>';
				$image_status_online = '<img border=\'0\' src=\'' . base_url() . 'images/grid/online.png\'>';
				$link_status_online = "<a href='" . base_url() . "index.php/batch/batch/go_offline/" . $row->ID_BATCH . "'>";
            }
			
			
            
				
			//modifikasi tanggal entry
			$tanggal = substr($row->TANGGAL_ENTRY, -2);
			$bulan = substr($row->TANGGAL_ENTRY, -5, 2);
			$tahun = substr($row->TANGGAL_ENTRY, -10, 4);
			$tanggal_entry = '' . $tanggal . '-' . $bulan . '-' . $tahun . '';
			
			//modifikasi tanggal selesai
			$tanggal_s = substr($row->TANGGAL_SELESAI, -2);
			$bulan_s = substr($row->TANGGAL_SELESAI, -5, 2);
			$tahun_s = substr($row->TANGGAL_SELESAI, -10, 4);
			$tanggal_selesai = '' . $tanggal_s . '-' . $bulan_s . '-' . $tahun_s . '';
			
			
			//modifikasi tanggal booking
			$tanggal_b = substr($row->TANGGAL_BOOKING, -2);
			$bulan_b = substr($row->TANGGAL_BOOKING, -5, 2);
			$tahun_b = substr($row->TANGGAL_BOOKING, -10, 4);
			$tanggal_booking = '' . $tanggal_b . '-' . $bulan_b . '-' . $tahun_b . '';
			
            $record_items[] = array(
                $row->ID_BATCH,
                $no = $no + 1,
				$tanggal_booking,
				$row->NAMA_LISENSI,
                $row->NAMA_BATCH,
				$tanggal_entry,
                $tanggal_selesai,
                '' . $status . '</a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
	//end booking batch
	

    public function form_batch() {
        $data['status'] = 'new';
        $data['title'] = 'Tambah Batch';

        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'batch/form_batch', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }
	

    public function insert_batch() {
		$this->session->set_userdata('tgl_mulai', $this->input->post('tanggal_mulai'));
		$this->session->set_userdata('tgl_selesai', $this->input->post('tanggal_selesai'));
		
        if ($this->cek_validasi() == FALSE) {
            $this->form_batch();
        }
        else{
			$tanggal_entry = $this->input->post('tanggal_mulai');
			$tanggal_selesai = $this->input->post('tanggal_selesai');
			
			// //start cek data batch
			// $sql = $this->db->query("SELECT COUNT(ID_BATCH) AS CEK FROM BATCH WHERE DATE(TANGGAL_ENTRY) >= DATE('" . $tanggal_entry . "') AND DATE(TANGGAL_ENTRY) <= DATE('" . $tanggal_selesai . "') ");
			// $cek = '';
			// foreach ($sql->result() as $rows) {
				// $cek = $rows->CEK;
			// }
			
			// //ubah menjadii >
			// if ($cek > 0) {
				// redirect('batch/batch/form_batch');
			// }
			// //end cek
			
			//else {
				$data = array(
					'ID_LISENSI' => $this->session->userdata('id_lisensi'),
					'NAMA_BATCH' => $this->input->post('nama_batch'),
					'JAM' => $this->input->post('jam'),
					'TEMPAT' => $this->input->post('tempat'),
					'TANGGAL_ENTRY' => $tanggal_entry,
					'TANGGAL_SELESAI' => $tanggal_selesai,
					'TANGGAL_BOOKING' => date('Y-m-d'),
					'STATUS' => 1
				);
				$this->batch_model->insert_batch($data);
				redirect('batch/batch');
			//}
        }
    }

    public function edit($id) {
        $data['status'] = 'edit';
        $record_peserta = $this->batch_model->selectone($id);
        foreach ($record_peserta->result() as $peserta) {
            $data['id_batch'] = $peserta->ID_BATCH;
            $data['nama_batch'] = $peserta->NAMA_BATCH;
            $data['jam'] = $peserta->JAM;
            $data['tempat'] = $peserta->TEMPAT;
            $data['tanggal_entry'] = $peserta->TANGGAL_ENTRY;
            $data['tanggal_selesai'] = $peserta->TANGGAL_SELESAI;
        }
        $data['title'] = 'Edit Batch';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'batch/form_batch', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    //update peserta
    public function update_batch($id) {
        if ($this->cek_validasi_edit() == FALSE) {
            redirect('batch/batch/edit/'.$id);
        }
        else{
			$tanggal_entry = $this->input->post('tanggal_mulai');
			$tanggal_selesai = $this->input->post('tanggal_selesai');
			$data = array(
				'NAMA_BATCH' => $this->input->post('nama_batch'),
                'JAM' => $this->input->post('jam'),
				'TEMPAT' => $this->input->post('tempat'),
				'TANGGAL_ENTRY' => $tanggal_entry,
				'TANGGAL_SELESAI' => $tanggal_selesai
			);
			$this->batch_model->update_batch($id, $data);
			redirect('batch/batch');
        }
    }

    public function delete($id) {
        $spt_ids_post_array = split(",", $this->input->post('items'));
        foreach ($spt_ids_post_array as $index => $id) {
            if (isset($id) && $id != '') {
                $this->batch_model->delete($id);
            }//end if
        }//end foreach
    }
	
	public function go_offline($id) {
        
                $this->batch_model->go_offline($id);
				$this -> index();
				redirect('sync');		
       
    }
	
	public function go_online($id) {
				$this->batch_model->go_online($id);
				redirect('sync/form_database');
                 
    }

    
    public function aprove($id){
        $records = $this->batch_model->selectone($id);
        foreach ($records->result() as $row) {
            if ($row->STATUS == '1') {
                $data = array('STATUS' => 2);
                $this->batch_model->update_batch($id, $data);
                redirect('batch/batch/all_batch');
            } else {
                $data = array('STATUS' => 1);
                $this->batch_model->update_batch($id, $data);
                redirect('batch/batch/all_batch');
            }
            
        }
    }
	

    function cek_validasi() {
        // Setting Rule
        $this->form_validation->set_rules('nama_batch', 'Nama Batch', 'required|callback_cek_nama_batch');
        $this->form_validation->set_rules('tanggal_mulai', 'Tanggal Mulai', 'required|callback_cek_tanggal_batch');
        $this->form_validation->set_rules('tanggal_selesai', 'Tanggal Selesai', 'required');
		
        // Setting Error Message
        $this->form_validation->set_message('required', 'Kolom %s harus diisi.');

        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }
	
	function cek_validasi_edit() {
        // Setting Rule
        $this->form_validation->set_rules('nama_batch', 'Nama Batch', 'required');
        $this->form_validation->set_rules('tanggal_mulai', 'Tanggal Mulai', 'required');
        $this->form_validation->set_rules('tanggal_selesai', 'Tanggal Selesai', 'required');
		
        // Setting Error Message
        $this->form_validation->set_message('required', 'Kolom %s harus diisi.');

        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }
	
	
	function cek_tanggal($value){
		$tanggal_entry = $this->session->userdata('tgl_mulai');
		$tanggal_selesai = $this->session->userdata('tgl_selesai');
		
		//start cek data batch
        $sql = $this->db->query("SELECT COUNT(ID_BATCH) AS CEK FROM BATCH WHERE TANGGAL_ENTRY >= '" . $tanggal_entry . "' AND TANGGAL_ENTRY <= '" . $tanggal_selesai . "' AND TANGGAL_SELESAI >= '".$tanggal_entry."'  AND TANGGAL_SELESAI <= '".$tanggal_selesai."' ");
        $cek = '';
        foreach ($sql->result() as $rows) {
            $cek = $rows->CEK;
        }
		
		//ubah menjadi >
        /* if ($cek > 0) {
            redirect('batch/batch/form_batch');
        } */
	
		if ($cek > 0) {
            $this->form_validation->set_message('cek_tanggal', 'Tanggal yang dipilih sudah digunakan.');
            return FALSE;
        } else {
            return TRUE;
        }
	}
	
	
	//cek_dropdown
	function cek_nama_batch($value){
		$nama_batch= $this->input->post('nama_batch');
		$sql = $this->db->query("SELECT COUNT(ID_BATCH) AS CEK FROM BATCH WHERE NAMA_BATCH = '" . $nama_batch . "'");
        $cek = '';
        foreach ($sql->result() as $rows) {
            $cek = $rows->CEK;
        }
		
		if ($cek > 0) {
            $this->form_validation->set_message('cek_nama_batch', 'Nama Batch Sudah Digunakan.');
            return FALSE;
        } else {
            return TRUE;
        }
	}
	
	
	//cek_tanggal_batch
	function cek_tanggal_batch($value){
		$tanggal_entry = $this->input->post('tanggal_mulai');
		$tanggal_selesai = $this->input->post('tanggal_selesai');
		//start cek data batch
		//$sql = $this->db->query("SELECT COUNT(ID_BATCH) AS CEK FROM BATCH WHERE DATE(TANGGAL_ENTRY) >= DATE('" . $tanggal_entry . "') AND DATE(TANGGAL_ENTRY) <= DATE('" . $tanggal_selesai . "')");
		
		$sql = $this->db->query("SELECT COUNT(ID_BATCH) AS CEK FROM BATCH WHERE DATE(TANGGAL_ENTRY) >= DATE('" . $tanggal_entry . "') AND DATE(TANGGAL_ENTRY) <= DATE('" . $tanggal_selesai . "') OR DATE(TANGGAL_ENTRY) = DATE('" . $tanggal_selesai . "') OR DATE(TANGGAL_SELESAI) = DATE('" . $tanggal_entry . "')");
		
		$cek = '';
		foreach ($sql->result() as $rows) {
			$cek = $rows->CEK;
		}
		
		//ubah menjadii >
		if ($cek > 0) {
			//redirect('batch/batch/form_batch');
			$this->form_validation->set_message('cek_tanggal_batch', 'Tanggal Pelatihan Sudah Digunakan.');
            return FALSE;
		}
		//end cek
		else {
            return TRUE;
        }
	}
	
}

/* End of file pegawai.php */
/* Location: ./application/controllers/pegawai.php */